﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Phone.Controls;
using Microsoft.Unsupported;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Xna.Framework.Media;
using com.google.zxing;
using System.Text;
using System.Windows.Markup;
using System.Globalization;
namespace iLikeWine
{
    public partial class PivotVerVinhos : PhoneApplicationPage
    {

        BitmapImage tintoImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_tinto.png", UriKind.Relative));
        BitmapImage brancoImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_branco.png", UriKind.Relative));
        BitmapImage roseImage = new BitmapImage(new Uri("/iLikeWine;component/Images/vinho_rose.png", UriKind.Relative));
       
        Uri targetPage;
        private const string conn = @"isostore:/iLikeWine.sdf";
        Wine w = new Wine();
        private static string selecao;

        object selectedItem;
           

        public PivotVerVinhos()
        {
             InitializeComponent();

           IList<Wine> lista = this.GetWines();


            foreach (Wine w in lista)
            {



                if (w.Tipo.Equals("Tinto"))
                {


                    w.ImageSource = tintoImage;

                }
                else if (w.Tipo.Equals("Branco"))
                {

                    w.ImageSource = brancoImage;

                }
                else
                {

                    w.ImageSource = roseImage;
                }

            }
            listBox1.ItemsSource = from Wine w in lista where w.Tipo.Equals("Tinto") 
                                   select new Wine 
                                   {
                                       ImageSource = w.ImageSource,
                                       Nome = w.Nome,
                                       Ano = w.Ano
                                   };
            listBox2.ItemsSource = from Wine w in lista
                                   where w.Tipo.Equals("Branco")
                                   select new Wine
                                   {
                                       ImageSource = w.ImageSource,
                                       Nome = w.Nome,
                                       Ano = w.Ano
                                   };
            listBox3.ItemsSource = from Wine w in lista
                                   where w.Tipo.Equals("Rose")
                                   select new Wine
                                   {
                                       ImageSource = w.ImageSource,
                                       Nome = w.Nome,
                                       Ano = w.Ano
                                   };
           
        }

        

       

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {

                selectedItem = (Wine)e.AddedItems[0];
                targetPage = new Uri("/Ver_CodBar_Search.xaml", UriKind.Relative);

                // reset selection of ListBox
                ((ListBox)sender).SelectedIndex = -1;

                // change  page navigation 
                NavigationService.Navigate(targetPage);
                FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
                root.DataContext = selectedItem;
            }

        }

        private void listBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {

                selectedItem = (Wine)e.AddedItems[0];
                targetPage = new Uri("/Ver_CodBar_Search.xaml", UriKind.Relative);

                // reset selection of ListBox
                ((ListBox)sender).SelectedIndex = -1;

                // change  page navigation 
                NavigationService.Navigate(targetPage);
                FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
                root.DataContext = selectedItem;
            }

        }

        private void listBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {

                selectedItem = (Wine)e.AddedItems[0];
                targetPage = new Uri("/Ver_CodBar_Search.xaml", UriKind.Relative);

                // reset selection of ListBox
                ((ListBox)sender).SelectedIndex = -1;

                // change  page navigation 
                NavigationService.Navigate(targetPage);
                FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
                root.DataContext = selectedItem;
            }

        }

        public IList<Wine> GetWines()
        {
            IList<Wine> lista = null;
            using (var ctx = new iLikeWineDataContext(conn))
            {
                IQueryable<Wine> query = ctx.Wines;
                //IQueryable<Wine> query = from ctx in ctx.Wines select w ;
                lista = query.ToList();
            }

            return lista;
        }

        private void Tap_LeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StackPanel rect = sender as StackPanel;
            // Change the size of the Rectangle.
            if (null != rect)
            {
                rect.Opacity = 0.5;


            }
        }
        private void Tap_LeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            StackPanel rect = sender as StackPanel;
            // Reset the dimensions on the Rectangle.
            if (null != rect)
            {
                rect.Opacity = 1.0;

            }
        }
        private void Tap_MouseLeave(object sender, MouseEventArgs e)
        {
            StackPanel rect = sender as StackPanel;
            // Finger moved out of Rectangle before the mouse up event.
            // Reset the dimensions on the Rectangle.


            if (null != rect)
            {
                rect.Opacity = 1.0;

            }
        }
    }
}